package com.njdj.gps.web.tag;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import com.njdj.gps.common.constants.NjdjConstants;
import com.njdj.gps.common.util.Utils;


/**
 * 分页标签
 */
public class PageSplitTag extends TagSupport {

	private String url = ""; // 页面指向地址

	private String pageNo = ""; // 当前页面，字符串型，由外面传入

	private String paramsStr = ""; // 组装后的参数字符串

	private int totalPages = 1; // 总页面数

	private int count = 0; // 总记录数

	private int intPageNo = 1; // 当前页面
	
	private String type ;

	private int pageSize = NjdjConstants.PAGE_SIZE_DEFAULT; // 每一页面显示的最大记录数

	public PageSplitTag() {
	}

	public int doStartTag() throws JspException {
		if (url == null) {
			url = "";
		}
		url = url.trim();

		HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
		Enumeration en = request.getParameterNames();
		StringBuffer param = new StringBuffer();
		while (en.hasMoreElements()) {
			String key = (String) en.nextElement();
			if ("pageNo".equals(key) || key.toLowerCase().startsWith("submit")||"pageSize".equals(key))
				continue;
			String value = Utils.trim(request.getParameter(key));
			if (value.equals(""))
				continue;
			param.append("&" + key + "=" + Utils.encodeStr(value));
		}
		paramsStr = param.toString();

		try {
			intPageNo = Utils.parseInt(pageNo, 1);
			if(intPageNo<1){
				intPageNo = 1;
			}
		} catch (Exception e) {
		}
		if (count % pageSize > 0) {
			totalPages = count / pageSize + 1;
		} else {
			totalPages = count / pageSize;
		}
		if (intPageNo > totalPages) {
			intPageNo = totalPages;
		}
		return (SKIP_BODY);
	}

	public int doEndTag() throws JspException {
		StringBuffer reStr = new StringBuffer();
		reStr.append(" <tr><td height='20'>&nbsp;</td>");
		reStr.append(" <td colspan='6'><table width='100%' border='0' cellspacing='1' cellpadding='0'>");
		reStr.append("<form name='splitPageForm' method='post' ");
		reStr.append("action='" + url + addParams(paramsStr) + "'>");
		reStr.append(" <tr><td width='300'>&nbsp;</td>");
		
		if (totalPages < 2) {
			reStr.append(" <td width='80'>首页</td>");
			reStr.append(" <td width='80'>上一页</td>");
			reStr.append(" <td width='80'>下一页</td>");		
			reStr.append(" <td width='80'>末页</td>");
		} else {
			if (intPageNo < 2) {
				reStr.append(" <td width='43'>首页</td>");
				reStr.append(" <td width='43'>上一页</td>");
				reStr.append(" <td width='40'>" + getUrl(intPageNo + 1,pageSize) + "下一页</a></td>");		
				reStr.append(" <td width='126'>" + getUrl(totalPages,pageSize) + "末页</a></td>");
			} else if (intPageNo == totalPages) {
				reStr.append(" <td width='43'>" + getUrl(1,pageSize) + "首页</a></td>");
				reStr.append(" <td width='43'>" + getUrl(intPageNo - 1,pageSize) + "上一页</a></td>");
				reStr.append(" <td width='40'>下一页</td>");		
				reStr.append(" <td width='126'>末页</td>");
			} else {
				reStr.append(" <td width='43'>" + getUrl(1,pageSize) + "首页</a></td>");
				reStr.append(" <td width='43'>" + getUrl(intPageNo - 1,pageSize) + "上一页</a></td>");
				reStr.append(" <td width='40'>" + getUrl(intPageNo + 1,pageSize) + "下一页</a></td>");		
				reStr.append(" <td width='126'>" + getUrl(totalPages,pageSize) + "末页</a></td>");
			}
		}
		
		
		reStr.append(" <td width='273'><font class='text24'>总条数:" + count + "  总页数:" + totalPages + "</font></td>");
		
		reStr.append(" <td align='right'><table width='216' border='0' cellpadding='0' cellspacing='0'>");		
		reStr.append(" <tr>");		
		reStr.append(" <td width='85'><font class='text24'>当前页:" + pageNo +"</font></td>");
		reStr.append(" <td width='31'><input name='pageNo' type='text' style='border:#dadada 1px solid;' size='4' maxlength='3'/></td>");		
		reStr.append(" <td width='49'><a href='#' onclick='splitPageForm.submit();'>");
		reStr.append(" 转到</a></td>");	
		reStr.append(" </tr>");
		reStr.append(" </table></td>");   
		
		reStr.append(" </tr>");	
		reStr.append(" </form>");
		reStr.append(" </table></td></tr>");
		JspWriter writer = pageContext.getOut();
		try {
			writer.println(new String(reStr.toString().getBytes()));
		} catch (Exception e) {
			throw new JspException(e.getMessage());
		}
		return (EVAL_PAGE);
	}

	private String getUrl(int pageNo,int pageSize) {
		return "<a href='" + dealUrl(url, pageNo,pageSize)
				+ "' >";
	}

	private String dealUrl(String url, int pageNo,int pageSize) {
		return url + "?pageNo=" + pageNo + "&pageSize=" + pageSize + paramsStr;
	}

	private String addParams(String params) {
		if (params == null || params.equals("")) {
			return "";
		}
		return "?" + params.substring(1);
	}

	public void release() {
		super.release();
	}

	public void setPageNo(String pageNo) {
		this.pageNo = pageNo;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

}
